const ui = {}
import {
  url_config
} from '@/common/config.js'
// 同步存储数据
ui.setStorage = (key, value) => {
  uni.setStorageSync(key, value)
}
// 同步获取数据
ui.getStorage = (key) => {
  return uni.getStorageSync(key)
}
// 移除指定key，不穿就是移除全部
ui.remove = (key) => {
  if (key) {
    uni.removeStorageSync(key)
  } else {
    uni.clearStorageSync()
  }
}

// 加载
ui.loading = (title) => {
  uni.showLoading({
    title: title
  })
}
// 提示框
ui.toast = (title) => {
  uni.showToast({
    title: title,
    icon: 'none'
  });
}
// 自定义弹框
ui.show = (title, content, showCancel = false) => {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title,
      content,
      showCancel,
      success: res => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}
// 获取状态栏的高度
ui.statusBarHeight = uni.getSystemInfoSync()['statusBarHeight'] + 10
// 返回事件
ui.back = () => {
  const pages = getCurrentPages() //判断跳转了多少页
  if (pages.length === 2) {
    uni.navigateBack({
      delta: 1,
    })
  } else if (pages.length === 1) {
    //如果没有上一页返回首页
    uni.switchTab({
      url: '/pages/mail/index',
    })
  } else {
    uni.navigateBack({
      delta: page || 1,
    })
  }
}
// 跳转
ui.push = (url, type = 'navigateTo') => {
  switch (type) {
    case 'navigateTo':
      uni.navigateTo({
        url
      })
      break;
    case 'redirectTo':
      uni.redirectTo({
        url
      })
      break;
    case 'reLaunch':
      uni.reLaunch({
        url
      })
      break;
    case 'switchTab':
      uni.switchTab({
        url
      })
      break;
  }
}


export default ui